package com.test.base.api;

import com.test.base.dao.NideshopAddressDao;
import com.test.base.entity.NideshopAddress;
import com.test.base.service.AddressService;
import com.test.base.service.RegionService;
import com.test.base.utils.Assist;
import com.test.base.vo.api.AddressEtxVo;
import com.test.base.vo.common.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "api/address")
@Api(description = "地址接口")
public class AddressApi {
    @Autowired
    private RegionService regionService;
    @Autowired
    private NideshopAddressDao nideshopAddressDao;
    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "获取用户的收货地址", notes = "")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultData<List<AddressEtxVo>> getAddressList(Long userId) {
        Assist region = new Assist();
        region.setRequires(Assist.andEq("user_id", userId));
        List<NideshopAddress> addressList = nideshopAddressDao.selectNideshopAddress(region);
        List<AddressEtxVo> result = new ArrayList<>();
        for (NideshopAddress address : addressList) {
            AddressEtxVo addressEtxVo = new AddressEtxVo();
            BeanUtils.copyProperties(address, addressEtxVo);
            addressEtxVo.setCityName(regionService.getRegionName(address.getCityId()));
            addressEtxVo.setProvinceName(regionService.getRegionName(address.getProvinceId()));
            addressEtxVo.setDistrictName(regionService.getRegionName(address.getDistrictId()));
            result.add(addressEtxVo);
        }
        return new ResultData<List<AddressEtxVo>>(result, null, true);
    }

    @ApiOperation(value = "获取收货地址的详情", notes = "")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public ResultData<AddressEtxVo> getAddressDetail(Long userId, Long addressId) {
        NideshopAddress region = new NideshopAddress();
        region.setId(addressId);
        region.setUserId(userId);
        NideshopAddress address = nideshopAddressDao.selectNideshopAddressByObj(region);
        AddressEtxVo addressEtxVo = new AddressEtxVo();
        BeanUtils.copyProperties(address, addressEtxVo);
        addressEtxVo.setCityName(regionService.getRegionName(address.getCityId()));
        addressEtxVo.setProvinceName(regionService.getRegionName(address.getProvinceId()));
        addressEtxVo.setDistrictName(regionService.getRegionName(address.getDistrictId()));
        return new ResultData<AddressEtxVo>(addressEtxVo, null, true);
    }

    @ApiOperation(value = "删除指定的收货地址", notes = "")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public ResultData delAddress(Long userId, Long addressId) {
        Assist region = new Assist();
        region.setRequires(Assist.andEq("user_id", userId));
        region.setRequires(Assist.andEq("id", addressId));
        int row = nideshopAddressDao.deleteNideshopAddress(region);
        if (row == 1) {
            return  ResultData.success();
        } else {
            return ResultData.fail("删除失败");
        }

    }

    /**
     * @returns
     */
    @ApiOperation(value = "添加或更新收货地址", notes = "")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResultData save(Long addressId, String name, String mobile, Integer provinceId, Integer cityId,
                           Integer districtId, String address, Long userId, int isDefault) {
        NideshopAddress addr = new NideshopAddress();
        addr.setUserId(userId);
        addr.setAddress(address);
        addr.setCityId(cityId);
        addr.setDistrictId(districtId);
        addr.setProvinceId(provinceId);
        addr.setName(name);
        addr.setMobile(mobile);
        addr.setIsDefault(isDefault);
        addr.setId(addressId);
        boolean bool = addressService.saveOrUpdate(addr);
        if (bool) {
            return  ResultData.success();
        } else {
            return ResultData.fail("删除失败");
        }
    }
}
